package com.nifostasky.delayed.producer;

import com.nifostasky.delayed.config.RabbitDelayedConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 延迟消息生产者
 */
@Component
public class Delay2Producer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendDelayedMsg(String msg, Integer delay) {
        MessageProperties mp = new MessageProperties();
        // 设置过期时间
        mp.setDelay(delay);
        Message message = new Message(msg.getBytes(), mp);
        rabbitTemplate.convertAndSend(RabbitDelayedConfig.EXCHANGE_NAME_DELAYED, "DELAY.MSG", message);
    }
}
